package com.ly.system.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author LY
 * @ClassName SystemController
 * @Date 2023/6/10 17:56
 * @Description 测试资源服务接口权限
 * @Version 1.0
 */
@RestController
public class SystemController {

    @PreAuthorize("hasAuthority('ADMIN')")
    @RequestMapping(value = "/getSys1",method = {RequestMethod.GET})
    public String getSys1(){
        final SecurityContext context = SecurityContextHolder.getContext();
        System.out.println(context.getAuthentication());

        return "sys1";
    }

    @PreAuthorize("hasAuthority('TEST')")
    @RequestMapping(value = "/getSys2",method = {RequestMethod.GET})
    public String getSys2(){
        return "sys2";
    }

    @RequestMapping(value = "/getSys3",method = {RequestMethod.GET})
    public String getSys3(){
        final SecurityContext context = SecurityContextHolder.getContext();
        System.out.println(context.getAuthentication().isAuthenticated());
        System.out.println(context.getAuthentication().getAuthorities());
        System.out.println(context.getAuthentication().getPrincipal());
        System.out.println(context.getAuthentication().getDetails());
        return "sys3";
    }

}